2 Dimensional Array Manipulations and Equalization for Clarity images


In [1]:
#insert your own parth here
import os
os.chdir('/Users/albertlee/NeuroCV/Module1/PCV/2Dhisteq')

In [2]:
### Run below if necessary

##import sys

##sys.path.append('/usr/local/lib/python2.7/site-packages')

import math
import csv,gc
import matplotlib
import numpy as np
import cv2

#%matplotlib
BINS = 32

In [3]:
import matplotlib.pyplot as plt
%matplotlib inline

In [4]:
from skimage import data, img_as_float
from skimage import exposure

In [5]:
import cv2

In [6]:
from PCV import tools
from PIL import Image
from numpy import *

def histeq(im,nbr_bins=256):
    """    Histogram equalization of a grayscale image. """
    
    # get image histogram
    imhist,bins = histogram(im.flatten(),nbr_bins,normed=True)
    cdf = imhist.cumsum() # cumulative distribution function
    cdf = 255 * cdf / cdf[-1] # normalize
    
    # use linear interpolation of cdf to find new pixel values
    im2 = interp(im.flatten(),bins[:-1],cdf)
    
    return im2.reshape(im.shape), cdf

In [18]:
im = array(Image.open('2Dexample.png'))
plt.imshow(np.asarray(im))


Out[18]:
<matplotlib.image.AxesImage at 0x1151f9b90>

In [8]:
im = array(Image.open('2Dexample.png').convert('L'))
plt.imshow(np.asarray(im))
im2,cdf = histeq(im)



In [9]:
histeqimage = Image.fromarray(im2)
histeqimage = histeqimage.convert('RGB')
plt.imshow(np.asarray(histeqimage))


Out[9]:
<matplotlib.image.AxesImage at 0x1119b9410>

In [10]:
im = array(Image.open('alexample.png'))
plt.imshow(np.asarray(im))


Out[10]:
<matplotlib.image.AxesImage at 0x112085f10>

In [11]:
im = array(Image.open('alexample.png').convert('L'))
im2,cdf = histeq(im)
plt.imshow(np.asarray(im))


Out[11]:
<matplotlib.image.AxesImage at 0x112e57e50>

In [12]:
histeqimage = Image.fromarray(im2)
histeqimage = histeqimage.convert('RGB')
plt.imshow(np.asarray(histeqimage))


Out[12]:
<matplotlib.image.AxesImage at 0x10de1da90>

In [13]:
im = array(Image.open('Fear199.png'))
plt.imshow(np.asarray(im))


Out[13]:
<matplotlib.image.AxesImage at 0x1119db610>

In [14]:
im = array(Image.open('Fear199.png').convert('L'))
im2,cdf = histeq(im)
plt.imshow(np.asarray(im))


Out[14]:
<matplotlib.image.AxesImage at 0x10eafccd0>

In [15]:
histeqimage = Image.fromarray(im2)
histeqimage = histeqimage.convert('RGB')
plt.imshow(np.asarray(histeqimage))


Out[15]:
<matplotlib.image.AxesImage at 0x113121d10>

In [16]:
im = np.asarray(im)
imgflat = im.reshape(-1)
fig = plt.hist(imgflat, bins=255)
plt.title('Original Histogram')
plt.show()



In [17]:
im = np.asarray(im2)
imgflat = im2.reshape(-1)
fig = plt.hist(imgflat, bins=255)
plt.title('Original Histogram')
plt.show()



In [ ]: